import { defineStore } from "pinia";
import * as api from "../api/permission"
// //引入完整的动态路由表
import routes from "../router/generatorRoutes";
import recursionRouters from "../utils/recursionRouters"
// 引入动态路由
import dynamicRoutes from "../router/dynamicRoutes";
// 静态
import staticRoutes from "../router/staticRoutes";
// 引入路由对象
import router from "../router"
import { useRoute } from "vue-router";
export const usePermission = defineStore('permission', {//persission唯一不可重复
    state() {
        return {
            menulist: [],
            logout:false,//是否退出
            collapse:false
        }
    },
    actions: {
        fetch_menulist() {
            // 发起请求，获取菜单
            return api.getMenuApi()
                .then(res => {
                    // console.log(res);
                    // console.log(routes,'123');
                    // 已经获取用户的路由
                    let userRoutes = recursionRouters(res.data.menuList, routes)
                    // console.log(userRoutes,99999);

                    this.menulist = userRoutes

                    //开始动态添加路由
                    //1.先从动态路由中,找到路径是" /"的路由对象
                    let target = dynamicRoutes.find(route => route.path === '/')
                    //2 将用户路由变成他的孩子
                    target.children = userRoutes
                    //3.静态路由也要添加到dynamicRoutes的孩子里面
                    target.children = [...target.children, ...staticRoutes]
                    //4.使用addRoute方法,将dynamicRoutes添加到路由的routes配置中
                    router.addRoute(target)
                    // console.log(target, '1123');
                })
        }

    }
})